package com.nebor.product.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.nebor.product.domain.bo.ProductInfoBO;
import com.nebor.product.domain.convert.ProductCategoryBoConverter;
import com.nebor.product.domain.convert.ProductInfoBoConverter;
import com.nebor.product.domain.service.ProductInfoDomainService;
import com.nebor.product.infra.basic.entity.ProductCategory;
import com.nebor.product.infra.basic.entity.ProductInfo;
import com.nebor.product.infra.basic.service.impl.ProductInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ProductInfoDomainServiceImpl implements ProductInfoDomainService {

    @Resource
    private ProductInfoServiceImpl productInfoService;

    @Override
    public void add(ProductInfoBO productInfoBO) {
        //打log
        if(log.isInfoEnabled()){
            log.info("ProductCategoryDomainServiceImpl.add.bo:{}", JSON.toJSONString(productInfoBO));
        }
        //转类型 从bo 到 infra
        ProductInfo productInfo = ProductInfoBoConverter.INSTANCE.convertBoToEntity(productInfoBO);
        //设置属性（业务层而非用户传递的属性）
        productInfo.setIsDeleted(String.valueOf(0));
        //调下层服务
        //mybatis的插入方法实现
        productInfoService.save(productInfo);
    }

    @Override
    public void delete(ProductInfoBO productInfoBO) {
        if(log.isInfoEnabled()){
            log.info("ProductCategoryDomainServiceImpl.delete.bo:{}", JSON.toJSONString(productInfoBO));
        }
        ProductInfo productInfo = ProductInfoBoConverter.INSTANCE.convertBoToEntity(productInfoBO);
        productInfoService.removeById(productInfo.getId());
    }

    @Override
    public void update(ProductInfoBO productInfoBO) {
        if(log.isInfoEnabled()){
            log.info("ProductCategoryDomainServiceImpl.delete.bo:{}", JSON.toJSONString(productInfoBO));
        }
        ProductInfo productInfo = ProductInfoBoConverter.INSTANCE.convertBoToEntity(productInfoBO);
        productInfoService.updateById(productInfo);
    }

    @Override
    public ProductInfoBO query(ProductInfoBO productInfoBO) {
        if(log.isInfoEnabled()){
            log.info("ProductCategoryDomainServiceImpl.query.bo:{}", JSON.toJSONString(productInfoBO));
        }
        ProductInfo productInfo = ProductInfoBoConverter.INSTANCE.convertBoToEntity(productInfoBO);
        ProductInfo info = productInfoService.getBaseMapper().selectById(productInfo.getId());
        ProductInfoBO bo = ProductInfoBoConverter.INSTANCE.convertEntityToBo(info);
        bo.setIsDeleted(String.valueOf(0));
        return bo;
    }

    @Override
    public List<ProductInfoBO> queryAll(ProductInfoBO productInfoBO) {

        List<ProductInfo> productInfoList = productInfoService.list();
        List<ProductInfoBO> productInfoBOList = ProductInfoBoConverter.INSTANCE.convertEntityToBoList(productInfoList);
        return productInfoBOList;
    }

}
